package com.jr.controller;

import com.alibaba.fastjson.JSON;
import com.jr.entity.Email;
import com.jr.service.EmailService;
import com.jr.service.impl.EmailServiceImpl;
import com.jr.util.Message;
import com.jr.util.ResultUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("*.email")
@MultipartConfig
public class EmailController extends HttpServlet {
    private EmailService emailService = new EmailServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1.获取servletPath /pages/admin/addVip.do
        String servletPath = request.getServletPath();
        // 2.获取到addVip
        String methodName = servletPath.substring(servletPath.lastIndexOf("/") + 1, servletPath.length() - 6);

        try {
            // 3.利用反射获取对应的方法对象
            Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 4.执行对应的方法
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }

    /**
     * 新增邮箱
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String emailDetail = request.getParameter("email");
        PrintWriter out = response.getWriter();
        Email email = new Email();
        email.setEmail_detail(emailDetail);
        email.setEnterprise_id((Integer) request.getSession().getAttribute("enterpriseId"));
        try {
            int i = emailService.addEmails(email);
            if (i > 0) {
                //System.out.println(emailDetail);
                out.write(JSON.toJSONString(new Message(403, "新增成功！")));
            } else {
                out.write(JSON.toJSONString(new Message(406, "新增失败！")));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 检查emailName是否存在
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void checkEmailName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        String emailName = request.getParameter("emailName");
        try {
            boolean b = emailService.checkEmailIsExist(emailName);
            if(b){
                out.write(JSON.toJSONString(new Message(440, "邮箱已存在")));
            } else{
                out.write(JSON.toJSONString(new Message(441, "邮箱可以使用!")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }
    /**
     * 展示查询到的邮箱
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void listAllEmail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        try {
            List<Email> allEmail = emailService.selectAllEmail((Integer) request.getSession().getAttribute("enterpriseId"));
            out.write(JSON.toJSONString(new ResultUtil("allEmail", allEmail)));
            //System.out.println(allEmail);
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }

    /**
     * 根据emailId删除邮箱(假删除.更新exist状态)
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateEmailExist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        Integer emailId = Integer.valueOf(request.getParameter("emailId"));
        try {
            int i = emailService.updateEmailExist(emailId);
            if (i > 0) {
                out.write(JSON.toJSONString(new Message(410, "删除成功")));
            } else {
                out.write(JSON.toJSONString(new Message(411, "删除失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            out.close();
        }
    }
    /**
     * 根据id设置地址默认值
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void setEmailDefault(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        Integer emailId = Integer.valueOf(request.getParameter("emailId"));

        try {
            int i1 = emailService.setAllEmailDefault();
            int i = emailService.setEmailDefaultById(emailId);
            if(i>0){
                out.write(JSON.toJSONString(new Message(420,"设置成功")));
            } else{
                out.write(JSON.toJSONString(new Message(421,"设置失败")));
            }
            out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            out.close();
        }
    }

}
